package com.storm.queue.register

import com.google.common.util.concurrent.ThreadFactoryBuilder
import com.storm.queue.core.DelayQueueContext
import com.storm.queue.model.Job
import org.slf4j.LoggerFactory
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy
import java.util.concurrent.TimeUnit

abstract class AbstractTopicRegister : CallBack {

    val log = LoggerFactory.getLogger(this.javaClass)

    override fun retryOutTimes(job: Job) {
        log.error("警告! Topic:{},Id:{} 已经重试仍然失败~ 请大佬关注一下 ", getTopic(), job.id)
    }

    /**线程池默认核心数 */
    private val corePoolSize = 2

    /**线程池默认最大线程数 */
    val maxPoolSize = 10

    /**回调方法默认超时时间 单位毫秒 */
    val methodTimeout: Long = 10000
    //注册Topic的名称
    abstract fun getTopic(): String

    //使用guava工具构建连接池
    private val TOPIC_THREADS = ThreadPoolExecutor(
            corePoolSize,
            maxPoolSize,
            60000,
            TimeUnit.MILLISECONDS,
            SynchronousQueue<Runnable>(),
            ThreadFactoryBuilder().setDaemon(true).setNameFormat(getTopic() + "-%d").build(),
            AbortPolicy()
    )

    fun getTOPIC_THREADS(): ThreadPoolExecutor {
        return TOPIC_THREADS
    }
}
